home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / indents.zip / io.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  23KB  |  679 lines

  1. /**
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that the above copyright notice and this paragraph are duplicated in all
  9.  * such forms and that any documentation, advertising materials, and other
  10.  * materials related to such distribution and use acknowledge that the
  11.  * software was developed by the University of California, Berkeley, the
  12.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  13.  * either University or Sun Microsystems may not be used to endorse or
  14.  * promote products derived from this software without specific prior written
  15.  * permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  17.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include "globals.h"
  21.  
  22. #ifndef lint
  23. # ifndef ANSIC
  24. static char     sccsid[] = "@(#)io.c    6.0 (Berkeley) 92/06/15";
  25. # endif         /* ANSIC */
  26. #endif          /* not lint */
  27.  
  28. #include <ctype.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #ifdef ANSIC
  33. # include <stdarg.h>
  34. # define bzero(achBuf, cbLength) memset(achBuf, 0, cbLength)
  35. static int      pad_output(int, int);
  36. #endif          /* ANSIC */
  37.  
  38. static int      comment_open = 0;
  39. static int      paren_target = 0;
  40.  
  41. /* dump_line is the routine that actually effects the printing of the new
  42.  * source. It prints the label section, followed by the code section with the
  43.  * appropriate nesting level, followed by any comments
  44.  */
  45. #ifdef ANSIC
  46. void            dump_line(void)
  47. #else           /* ANSIC */
  48. void            dump_line()
  49. #endif          /* ANSIC */
  50. {
  51.     register int    cur_col;
  52.     register int    target_col;
  53.     static int      not_first_line = 0; /* = 0 ? PETER */
  54.  
  55.     if (ps.procname[0]) {
  56.         if (troff) {
  57.             if (comment_open) {
  58.                 comment_open = 0;
  59.                 fprintf(output, ".*/\n");
  60.             }
  61.             fprintf(output, ".Pr \"%s\"\n", ps.procname);
  62.         }
  63.         ps.ind_level = 0;
  64.         ps.procname[0] = 0;
  65.     }
  66.     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
  67.         if (suppress_blanklines > 0)
  68.             suppress_blanklines--;
  69.         else {
  70.             ps.bl_line = true;
  71.             n_real_blanklines++;
  72.         }
  73.     } else if (!inhibit_formatting) {
  74.         suppress_blanklines = 0;
  75.         ps.bl_line = false;
  76.         if (prefix_blankline_requested && not_first_line)
  77.             if (swallow_optional_blanklines) {
  78.                 if (n_real_blanklines == 1)
  79.                     n_real_blanklines = 0;
  80.             } else {
  81.                 if (n_real_blanklines == 0)
  82.                     n_real_blanklines = 1;
  83.             }
  84.         while (--n_real_blanklines >= 0)
  85.             putc('\n', output);
  86.         n_real_blanklines = 0;
  87.         if (ps.ind_level == 0) {
  88.             if (!btype_3)
  89.                 ps.ind_stmt = 0;/* this is a class A kludge. don't do
  90.                                  * additional statement indentation if we are
  91.                                  * at bracket level 0 */
  92.             else if (*s_code != '{')
  93.                 ps.ind_stmt = 0;/* this one is a class AA kludge: defeat the
  94.                                  * class A kludge if the statement is '{' */
  95.         }
  96.         if (e_lab != s_lab || e_code != s_code)
  97.             ++code_lines;       /* keep count of lines with code */
  98.  
  99.  
  100.         if (e_lab != s_lab) {   /* print lab, if any */
  101.             if (comment_open) {
  102.                 comment_open = 0;
  103.                 fprintf(output, ".*/\n");
  104.             }
  105.             while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
  106.                 e_lab--;
  107.             cur_col = pad_output(1, compute_label_target());
  108.             fprintf(output, "%.*s", e_lab - s_lab, s_lab);
  109.             cur_col = count_spaces(cur_col, s_lab);
  110.         } else
  111.             cur_col = 1;        /* there is no label section */
  112.  
  113.         ps.pcase = false;
  114.  
  115.         if (s_code != e_code) { /* print code section, if any */
  116.             register char  *p = NULL;
  117.  
  118.             if (comment_open) {
  119.                 comment_open = 0;
  120.                 fprintf(output, ".*/\n");
  121.             }
  122.             target_col = compute_code_target();
  123.             {
  124.                 register int    i;
  125.  
  126.                 for (i = 0; i < ps.p_l_follow; i++)
  127.                     if (ps.paren_indents[i] >= 0)
  128.                         ps.paren_indents[i] = -(short) (ps.paren_indents[i] + target_col);
  129.             }
  130.             cur_col = pad_output(cur_col, target_col);
  131.             for (p = s_code; p < e_code; p++)
  132.                 if (*p == (char) 0200)
  133.                     fprintf(output, "%d", target_col * 7);
  134.                 else
  135.                     putc(*p, output);
  136.             cur_col = count_spaces(cur_col, s_code);
  137.         }
  138.         if (s_com != e_com)
  139.             if (troff) {
  140.                 int             all_here = 0;
  141.                 register char  *p = NULL;
  142.  
  143.                 if (ps.cc_comment)
  144.                     all_here++;
  145.                 else if (e_com[-1] == '/' && e_com[-2] == '*')
  146.                     e_com -= 2, all_here++;
  147.                 while (e_com > s_com && e_com[-1] == ' ')
  148.                     e_com--;
  149.                 *e_com = 0;
  150.                 p = s_com;
  151.                 while (*p == ' ')
  152.                     p++;
  153.                 if (p[0] == '/' && (p[1] == '*' || p[1] == '/'))
  154.                     p += 2, all_here++;
  155.                 else if (p[0] == '*')
  156.                     p += p[1] == '/' ? 2 : 1;
  157.                 while (*p == ' ')
  158.                     p++;
  159.                 if (*p == 0)
  160.                     goto inhibit_newline;
  161.                 if (comment_open < 2 && ps.box_com) {
  162.                     comment_open = 0;
  163.                     fprintf(output, ".*/\n");
  164.                 }
  165.                 if (comment_open == 0) {
  166.                     if ('a' <= *p && *p <= 'z')
  167.                         *p += 'A' - 'a';
  168.                     if (e_com - p < 50 && all_here == 2) {
  169.                         register char  *follow = p;
  170.  
  171.                         fprintf(output, "\n.nr C! \\w\1");
  172.                         while (follow < e_com) {
  173.                             switch (*follow) {
  174.                             case '\n':
  175.                                 putc(' ', output);
  176.                             case 1:
  177.                                 break;
  178.                             case '\\':
  179.                                 putc('\\', output);
  180.                             default:
  181.                                 putc(*follow, output);
  182.                             }
  183.                             follow++;
  184.                         }
  185.                         putc(1, output);
  186.                     }
  187.                     fprintf(output, "\n./* %dp %d %dp\n",
  188.                             ps.com_col * 7,
  189.                           (s_code != e_code || s_lab != e_lab) - ps.box_com,
  190.                             target_col * 7);
  191.                 }
  192.                 comment_open = 1 + ps.box_com;
  193.                 while (*p) {
  194.                     if (*p == BACKSLASH)
  195.                         putc(BACKSLASH, output);
  196.                     putc(*p++, output);
  197.                 }
  198.             } else {            /* print comment, if any */
  199.                 register int    target = ps.com_col;
  200.                 register char  *com_st = s_com;
  201.  
  202.                 target += ps.comment_delta;
  203.                 while (*com_st == '\t')
  204.                     com_st++, target += tabsize;    /* JHT 22oct89 */
  205.                 while (target <= 0)
  206.                     if (*com_st == ' ')
  207.                         target++, com_st++;
  208.                     else if (*com_st == '\t') {
  209.                         target += (tabsize - ((target - 1) % tabsize)); /* JHT 22oct89 */
  210.                         com_st++;
  211.                     } else
  212.